package com.video.myapplication;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.video.myapplication.adapter.FileAdapter;
import com.video.myapplication.util.FileSystem;
import com.video.myapplication.util.Video;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView = null;
    private List<Video> data;
    private FileSystem fileSystem = null;
    private Button button;
    private FileAdapter mAdapter = null;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.file);

        if(isPermissionGranted()){
            initView();
        }
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        initView();
    }

    private void initVideo(){
        fileSystem = new FileSystem(this);
        this.data = fileSystem.getVideos();
        Log.e("fuweicong", "视频读取完毕" + this.data.size());
    }

    public void initView(){
        initVideo();
        recyclerView = findViewById(R.id.file_list_view);
        button = findViewById(R.id.enter);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this,3);
        recyclerView.setLayoutManager(gridLayoutManager);
        mAdapter = new FileAdapter(this);
        recyclerView.setAdapter(mAdapter);

        mAdapter.setDataSource(data);
        initEvent();
    }

    public void initEvent(){
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, VideoActivity.class);
                int index = mAdapter.getSelectIndex();
                Log.e("fuweicong select index", String.valueOf(index));
                intent.putExtra("video", data.get(index).getPath());
                startActivity(intent);
            }
        });
    }


    public boolean isPermissionGranted() {
        if (Build.VERSION.SDK_INT >= 23) {
            if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
                    && ActivityCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED
                    && ActivityCompat.checkSelfPermission(this,Manifest.permission.INTERNET) == PackageManager.PERMISSION_GRANTED
            ) {
                Log.e("chenjunda","Permission is granted");
                System.out.println("Permission is granted");
                return true;
            } else {
                Log.e("chenjunda","Permission is revoked");
                System.out.println("Permission is revoked");
                ActivityCompat.requestPermissions(this, new String[]{
                        android.Manifest.permission.CAMERA,
                        android.Manifest.permission.READ_EXTERNAL_STORAGE,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.RECORD_AUDIO,
                        Manifest.permission.INTERNET
                }, 1);
                return false;
            }
        } else { //permission is automatically granted on sdk<23 upon installation
            Log.e("chenjunda","Permission is granted");
            System.out.println("Permission is granted");
            return true;
        }
    }
}